﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace estiglp1112_5946_csharp
{
    public partial class StatistcGui : Form
    {
        /// <summary>
        /// Interface grafica para visualização dos dados
        /// </summary>
        List<Teacher> teachers; 
        int statistic;
        public StatistcGui(string DB, string Table,int stat)
        {
            InitializeComponent();
            this.statistic = stat;
            this.openDataBase(DB, Table);
        }        
        /// <summary>
        /// Abre a base de dados e gera a estrutura de dados, preenche a primeira lista com os anos
        /// </summary>
        /// <param name="db"></param>
        /// <param name="table"></param>
        private void openDataBase(string db, string table)
        {
            SQLHandler sql = new SQLHandler(db);
            this.teachers = sql.getData(table);
            this.getYears(Years);
        }

        /// <summary>
        /// Preenche a lista dos anos
        /// </summary>
        /// <param name="list">Lista a ser preenchida</param>
        private void getYears(ListBox list)
        {
            list.Items.Clear();
            var years = from te in this.teachers
                                 group new { te.Year } by te.Year into g
                                 select g;
            foreach (var item in years)
            {
                list.Items.Add(item.Key);
            }            
            list.Sorted = true; 
        }
        /// <summary>
        /// procura um escola por ano
        /// </summary>
        /// <param name="year">Ano a procurar</param>
        /// <param name="list">Lista a preencher</param>
        private void getEstablismentsInYear(string year, ListBox list)
        {
            list.Items.Clear();
            var establishments = from te in this.teachers
                        where te.Year == Years.SelectedItem.ToString()
                        group new {te.Establishment} by te.Establishment into g
                        select g;
            foreach (var item in establishments)
            {
                list.Items.Add(item.Key);
            }
            list.Sorted = true; 
        }
        /// <summary>
        /// Evento ao clickar na lista de anos
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Years_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                this.getEstablismentsInYear(Years.SelectedItem.ToString(), Establishments); 
            }
            catch { }
                      
        }
        /// <summary>
        /// Devolve categorias ou graus numa escola
        /// </summary>
        /// <param name="scool"></param>
        /// <param name="list"></param>
        private void getCatOrDegree(string scool, ListBox list)
        {
            list.Items.Clear();
            if (statistic == 4)
            {
                var catOrDegree = from te in this.teachers
                                  where te.Year == Years.SelectedItem.ToString()
                                  && te.Establishment == Establishments.SelectedItem.ToString()
                                  group new { te.Catgory } by te.Catgory into g
                                  select g;
                foreach (var item in catOrDegree)
                {
                    list.Items.Add(item.Key);
                }
            }
            else if (this.statistic == 2)
            {
                var catOrDegree = from te in this.teachers
                                  where te.Year == Years.SelectedItem.ToString()
                                  && te.Establishment == Establishments.SelectedItem.ToString()
                                  group new { te.Degree } by te.Degree into g
                                  select g;
                foreach (var item in catOrDegree)
                {
                    list.Items.Add(item.Key);
                }
            }
            list.Sorted = true;
        }

        /// <summary>
        /// evento de clcik na lista de establecimentos
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Establishments_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                this.getCatOrDegree(Establishments.SelectedItem.ToString(), CatOrDeg);
            }
            catch { }
        }

        /// <summary>
        /// Crocura um item na estrutura de dados
        /// </summary>
        /// <param name="item">Item a ser procurado</param>
        /// <param name="list">Lista a preencher com os dados finais</param>
        private void getStatistic(string item, ListBox list)
        {
            list.Items.Clear();
            foreach (var x in Years.Items)
            {                
                List<Teacher> count = this.getTeachers(x.ToString());
                list.Items.Add(x + "-->" + count.Count);
            }
            list.Sorted = true;
        }
        /// <summary>
        /// Devolve lista de professores
        /// </summary>
        /// <returns>Lista de professores </returns>
        private List<Teacher> getTeachers(string year)
        {
            if (this.statistic == 4)
            {
                var grid = from te in this.teachers
                           where te.Year == year
                               && te.Establishment == Establishments.SelectedItem.ToString()
                               && te.Catgory == CatOrDeg.SelectedItem.ToString()                                 
                               select te;              
               return grid.ToList();
            }
            else if(this.statistic == 2)
            {
                var grid = from te in this.teachers
                           where te.Year == year
                               && te.Establishment == Establishments.SelectedItem.ToString()
                               && te.Degree == CatOrDeg.SelectedItem.ToString()
                           select te;
                return grid.ToList();
            }
            return null;
        }
        /// <summary>
        /// evento de click na lista de categorias ou graus
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CatOrDeg_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                this.getStatistic(CatOrDeg.SelectedItem.ToString(), stat);
                dataGrid.DataSource = this.getTeachers(Years.SelectedItem.ToString());
            }
            catch { }
        }       
    }
}
